#include <REG52.H>
#include <INTRINS.H>
#include "eeprom.h"
#define uchar unsigned char
#define uint unsigned int
uchar code table[] = {
    0x3F, //"0"
    0x06, //"1"
    0x5B, //"2"
    0x4F, //"3"
    0x66, //"4"
    0x6D, //"5"
    0x7D, //"6"
    0x07, //"7"
    0x7F, //"8"
    0x6F, //"9"
    0x77, //"A"
    0x7C, //"B"
    0x39, //"C"
    0x5E, //"D"
    0x79, //"E"
    0x71, //"F"
    0x76, //"H"
    0x38, //"L"
    0x37, //"n"
    0x3E, //"u"
    0x73, //"P"
    0x5C, //"o"
    0x40, //"-"
    0x00  //熄灭
};
sbit qian = P2 ^ 4;
sbit bai = P2 ^ 5;
sbit shi = P2 ^ 6;
sbit ge = P2 ^ 7;

// 串口发送
void Uart_Init()
{
    SCON = 0x50;
    PCON = 0;
    TMOD |= 0x20;
    TH1 = 0xfd, TL1 = 0xfd; //9600
    TR1 = 1;                //timer1定时器打开
    ES = 1;                 //串口中断
}
// 串口发送一个字节
void UartSendByte(uchar buff)
{
    SBUF = buff;
    while (busy)
        ;
    busy = 1;
}

void main(void)
{
    uchar V = 0, i = 0, j = 0;
    uchar temp[32];
    uint addr = IAP_ADDRESS;
    Uart_Init();
    EA = 1;
    while (V != 0xff)
    {
        V = IapReadByte(addr++);
        temp[i] = V;
        UartSendByte(temp[i++]);
    }
    // 写入内容
    // addr = IAP_ADDRESS;    //地址重新赋值
    // IapEraseSector(addr);  //擦除扇区
    // j = CheckFF();         //检查是否擦完成
    // UartSendByte(j + '0'); //1完成；0出错
    // if (j)
    // {
    //     IapWriteStr(addr, "testbyduliang\n2021-9-21\n"); //地址写入新数据
    //     while (V != 0xff)                                //如果不为空
    //     {
    //         V = IapReadByte(addr++); //从地址读取一个字节并移到下一个地址
    //         temp[i] = V;             //存入数组
    //         UartSendByte(temp[i++]); //从数组中取出串口发出
    //     }
    // }

    while (1)
    {
    }
}
void uartInterrupt() interrupt 4
{
    if (RI)
    {
        RI = 0;
    }
    if (TI)
    {
        TI = 0;
        busy = 0;
    }
}
void IapWriteStr(uint addr, uchar *str)
{
    uchar i = 0;
    while (str[i] != '\0')
    {
        IapWriteByte(addr++, str[i++]);
    }
}
